package com.dhnsoft.education.controller.edu;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhnsoft.education.entity.edu.EduTeacher;
import com.dhnsoft.education.entity.edu.vo.TeacherQuery;
import com.dhnsoft.education.service.edu.EduTeacherService;
import com.dhnsoft.education.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 代浩楠
 * @since 2022-01-24
 */

@RestController
@RequestMapping("/eduservice/teacher")
@Api(description="讲师管理")
//@CrossOrigin
public class EduTeacherController {
    //http://localhost:8001/eduservice/teacher/findAll
    @Autowired
    private EduTeacherService eduTeacherService;
    //1.查询讲师表中所有数据
    //rest风格
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        for (EduTeacher eduTeacher : list) {
            System.out.println(eduTeacher);
        }
        return R.ok().data("items",list);
    }

    //2.逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("removeTeacher/{id}")
    public R removeTeacher(@ApiParam(name="id",value="讲师ID",required = true) @PathVariable String id){ //id需要通过路径传递

        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }


    //3.分页查询讲师方法
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit){
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合数
//        try {
//            int i=1/0;
//        }catch (Exception e){
//            throw new EduException(20001,"执行自定义异常处理....");
//        }
//        Map map = new HashMap<>();
//        map.put("total",total);
//        map.put("row",records);
        return R.ok().data("total",total).data("row",records);
    }

    //4.条件查询带分页的方法
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //动态sql
        //判断条件值是否为空，如果不为空拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);//第一个值为数据库表中字段值，第二个值为实值
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.lt("gmt_creat",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.gt("gmt_modified",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合数
        return R.ok().data("total",total).data("rows",records);
    }
    //添加讲师接口方法
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        System.out.println(eduTeacher);
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //根据讲师id查询
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable long id){
        EduTeacher byId = eduTeacherService.getOne(new QueryWrapper<EduTeacher>().eq("id",id));
        return R.ok().data("teacher",byId);
    }

    //根据讲师id修改讲师数据
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.updateById(eduTeacher);
        if (save){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

